const qrcode = require('../../static/js/qrcode');

Page({
  data: {
    info: null,
    qrcode_w: 130
  },
  onLoad: function(options) {
    const W = wx.getSystemInfoSync().windowWidth;
    const rate = 750.0 / W;
    //利用比例将260rpx转换为px
    const qrcode_w = 260 / rate;
    wx.util.request('user.coupon.detail', {
      body: {id: options.id}
    }).then(info => {
      info.general = info.couponType.range_type == 1;
      info.reduce = info.type == 1;
      info.reduceTop = info.reduce ? ` 减 ${info.money}` : ` 享 ${info.discount / 10} 折`;
      info.startTime = wx.time.toDate(info.start_time, 'Y-M-D');
      info.endTime = wx.time.toDate(info.end_time, 'Y-M-D');
      this.setData({
        info: info,
        qrcode_w: qrcode_w
      }, () => {
        this.QRCode(this.data.info.code);
      });
    });
  },
  product: function (e) {
    wx.navigateTo({
      url: `../product/detail?id=${e.currentTarget.dataset.id}`,
    })
  },
  QRCode(text) {
    new qrcode('QRCode', {
      text: text,
      width: this.data.qrcode_w,
      height: this.data.qrcode_w,
      colorDark: '#333',
      colorLight: '#FFF',
      correctLevel: qrcode.CorrectLevel.H
    });
  }
})